<template>
  <div>
    <van-nav-bar title="个人信息" class="page-nav-bar">
      <van-icon slot="left" name="arrow-left" @click="$router.back()">
      </van-icon>
    </van-nav-bar>
    <!-- 设置隐藏域 -->
    <!-- <img :src="imgPath" alt=""> -->
    <input type="file" hidden ref="fileRef" @change="onFileChange">
    <!-- 点击头像，触发这个隐藏域 -->
    <van-cell title="头像" is_link @click="$refs.fileRef.click()">
      <van-image round class="avatar" fit="cover" :src="user.photo"></van-image>
    </van-cell>
    <!-- 打开弹层 -->
    <van-cell title="姓名" is_link :value="user.name" @click="isUpdateNameShow=true" />
    <van-cell title="性别" is_link :value="user.gender === 1 ? '女' : '男'"  @click="isUpdateGenderShow=true"/>
    <van-cell title="生日" is_link :value="user.birthday"  @click="isUpdateBirthdayShow=true"/>
    <!-- 修改姓名的tanc -->
    <van-popup
      v-model="isUpdateNameShow"
      position="bottom"
      :style="{ height: '100%' }"
      >
      <!-- 父组件传值给子组件 -->
      <up-date-name
      @close="isUpdateNameShow=false"
      v-model="user.name"
      v-if="isUpdateNameShow"
      ></up-date-name>
      </van-popup
    >
    <!-- 修改性别的弹层 -->
    <van-popup
      v-model="isUpdateGenderShow"
      position="bottom"
      :style="{ height: '50%' }"
      >
      <!-- 父组件传值给子组件 -->
      <up-date-gender
      @close="isUpdateGenderShow=false"
      v-model="user.gender"
      v-if="isUpdateGenderShow"
      ></up-date-gender>
      </van-popup
    >
    <!-- 修改生日的弹层 -->
    <van-popup
      v-model="isUpdateBirthdayShow"
      position="bottom"
      >
      <!-- 父组件传值给子组件 -->
     <up-date-birthday
     v-model="user.birthday"
     v-if="isUpdateBirthdayShow"
     @close="isUpdateBirthdayShow=false"
     >
     </up-date-birthday>
      </van-popup>
      <!-- 修改头像的弹层 -->
      <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
      >
      <!-- 父组件传值给子组件 -->
      <up-date-photo
      @update-photo="user.photo=$event"
      :img="imgPath"
       @close="isUpdatePhotoShow=false"
      v-if="isUpdatePhotoShow">
      </up-date-photo>
      </van-popup
    >
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import upDateName from './compoments/update-name.vue'
import upDateGender from './compoments/update-gender.vue'
import upDateBirthday from './compoments/update-birthday.vue'
import UpDatePhoto from './compoments/update-photo.vue'
export default {
  name: 'UserProfile',
  components: {
    upDateName,
    upDateGender,
    upDateBirthday,
    UpDatePhoto
  },
  data () {
    return {
      user: { },
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      imgPath: ''
    }
  },
  async created () {
    const res = await getUserProfile()
    console.log(res)
    this.user = res.data.data
  },
  mounted () {

  },
  methods: {
    onFileChange (e) {
      // 在methods中，this执行组件本身
      this.imgPath = URL.createObjectURL(e.target.files[0])
      e.target.value = null
      this.isUpdatePhotoShow = true
    }
  }
}
</script>

<style lang="less" scoped>
.avatar {
  width: 60px;
  height: 60px;
}
.van-popup {
  background-color: #efefef;
}
</style>